What is is-accessor-descriptor?
The is-accessor-descriptor npm package is used to check if an object property descriptor defines an accessor descriptor. An accessor descriptor is one that includes getter and/or setter functions, as opposed to a data descriptor which contains a value and is writable.
What are is-accessor-descriptor's main functionalities?
Check if a descriptor is an accessor descriptor
This feature allows you to verify if a given property descriptor from an object is an accessor descriptor. It returns true if the descriptor has a get or set key, and false otherwise.
{"isAccessorDescriptor": require('is-accessor-descriptor');
var descriptor = Object.getOwnPropertyDescriptor({get foo() {}}, 'foo');
console.log(isAccessorDescriptor(descriptor)); //=> true
}
Check if an object is an accessor descriptor
This feature allows you to check if a plain object mimics the structure of an accessor descriptor. It is useful for validation purposes when you have an object that should represent a descriptor and you want to ensure it is an accessor descriptor.
{"isAccessorDescriptor": require('is-accessor-descriptor');
console.log(isAccessorDescriptor({get: function() {}})); //=> true
console.log(isAccessorDescriptor({set: function() {}})); //=> true
console.log(isAccessorDescriptor({value: 123})); //=> false
}
Other packages similar to is-accessor-descriptor
is-data-descriptor
This package is similar to is-accessor-descriptor but checks for data descriptors instead. A data descriptor is an object with a value property and optionally writable, enumerable, and configurable properties. It is the counterpart to is-accessor-descriptor, which checks for accessor descriptors.
is-descriptor
is-descriptor is a more general package that checks if an object is a valid property descriptor, which can be either a data descriptor or an accessor descriptor. It is a superset of is-accessor-descriptor and is-data-descriptor functionalities.
is-plain-object
While not specifically for checking descriptors, is-plain-object can be used to determine if an object is plain (i.e., created by the Object constructor or one with a null prototype). This can be a preliminary check before further validation as a descriptor.
is-accessor-descriptor
Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
Examples
var isAccessor = require('is-accessor-descriptor');
var assert = require('assert');
assert.equal(isAccessor({ get: function() {} }), true);
You may also pass an object and property name to check if the property is an accessor:
assert.equal(isAccessor({ bar: {} }, 'bar'), true);
Examples
false
when not an object
assert.equal(isAccessor('a'), false);
assert.equal(isAccessor(null), false);
true
when the object has valid properties
and the properties all have the correct JavaScript types:
assert.equal(isAccessor({ get() {}, set() {} }), true);
assert.equal(isAccessor({ get() {} }), true);
assert.equal(isAccessor({ set() {} }), true);
false
when the object has invalid properties
assert.equal(isAccessor({ get() {}, set() {}, enumerable: 'baz' }), false);
assert.equal(isAccessor({ get() {}, writable: true }), false);
assert.equal(isAccessor({ get() {}, value: true }), false);
false
when an accessor is not a function
isAccessor({ get() {}, set: 'baz' });
isAccessor({ get: 'foo', set() {} });
isAccessor({ get: 'foo', bar: 'baz' });
isAccessor({ get: 'foo', set: 'baz' });
false
when a value is not the correct type
isAccessor({ get() {}, set() {}, enumerable: 'foo' });
isAccessor({ set() {}, configurable: 'foo' });
isAccessor({ get() {}, configurable: 'foo' });
Related projects
You might also be interested in these projects:
- is-data-descriptor: Returns true if a value has the characteristics of a valid JavaScript data descriptor.
- is-descriptor: Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for… more
- is-object: Returns true if the value is an object and not an array or null.
Tests
Simply clone the repo, npm install
, and run npm test